<?php
class editor_plants
{
    public $id;
    public $html;

    public $cats = false;
    public $catoption = null;

    public $json_place = '';

    function _print($txt)
    {
        $this->html.=$txt."\n";
    }

    function  __construct($id = 0) {
        $this->json_place = '/home/ingridients/json/plants.json';
        $this->id = (int)$id;
        $this->write();
        $this->_print('<style>
.textf{width:100px}
td{border:1px solid #eee;}
</style>');
    }

    function write()
    {
        if(isset($_POST['json']))
        {
            $query = 'SELECT * FROM `admin__plants` LIMIT 100';
            $arr = _database::toarray($query);
            $out = null;
            foreach($arr as $row)
            {
                $out[$row['id']] = $row;
            }
            file_put_contents($this->json_place,json_encode($out));
            include '/home/ingridients/admin/lib.php';
            libgen::_add('plants', $out);
            print $this->json_place;
        }
        if(isset($_POST['save']))
        {

            foreach($_POST['id'] as $id)
            {
                if($id == 0 && $_POST['cost'][0])
                {
                    $query = 'INSERT INTO `admin__plants` SET
`name`=\''.mysql_escape_string($_POST['name'][0]).'\',
`cost`=\''.mysql_escape_string($_POST['cost'][0]).'\',
`goldcost`=\''.mysql_escape_string($_POST['goldcost'][0]).'\',
`product_id`=\''.mysql_escape_string($_POST['product_id'][0]).'\',
`product_num`=\''.mysql_escape_string($_POST['product_num'][0]).'\',
`grow_time`=\''.mysql_escape_string($_POST['grow_time'][0]).'\',
`asset`=\''.mysql_escape_string($_POST['asset'][0]).'\'';
                }
                else if($id)
                {
                    $query = 'INSERT INTO `admin__plants` SET
`id`=\''.mysql_escape_string($_POST['id'][$id]).'\',
`cost`=\''.mysql_escape_string($_POST['cost'][$id]).'\',
`goldcost`=\''.mysql_escape_string($_POST['goldcost'][$id]).'\',
`product_id`=\''.mysql_escape_string($_POST['product_id'][$id]).'\',
`product_num`=\''.mysql_escape_string($_POST['product_num'][$id]).'\',
`grow_time`=\''.mysql_escape_string($_POST['grow_time'][$id]).'\',
`name`=\''.mysql_escape_string($_POST['name'][$id]).'\',
`asset`=\''.mysql_escape_string($_POST['asset'][$id]).'\'
                    ON DUPLICATE KEY UPDATE
`id`=\''.mysql_escape_string($_POST['id'][$id]).'\',
`cost`=\''.mysql_escape_string($_POST['cost'][$id]).'\',
`goldcost`=\''.mysql_escape_string($_POST['goldcost'][$id]).'\',
`product_id`=\''.mysql_escape_string($_POST['product_id'][$id]).'\',
`product_num`=\''.mysql_escape_string($_POST['product_num'][$id]).'\',
`grow_time`=\''.mysql_escape_string($_POST['grow_time'][$id]).'\',
`name`=\''.mysql_escape_string($_POST['name'][$id]).'\',
`asset`=\''.mysql_escape_string($_POST['asset'][$id]).'\'
';
                }

                _database::query($query);

            }
        }
    }

    function getTileList()
    {
        $query = 'SELECT * FROM `admin__plants` LIMIT 100';
        return _database::toarray($query);
    }

    function print_tile($tile)
    {

        $this->_print('<tr>');
        $this->_print('<td valign="top" style="width:100px;height:100px;background:url(/img/plants/'.$tile['asset'].'_ready.png) 0 0 no-repeat">&#160;</td>');
        $this->_print('<td valign="top"><input style="width:200px" name="name['.(int)$tile['id'].']" value="'.$tile['name'].'" /></td>');
        $this->_print('<td valign="top"><input style="width:50px" name="cost['.(int)$tile['id'].']" value="'.($tile['cost']).'"/></td>');
        $this->_print('<td valign="top"><input style="width:50px" name="goldcost['.(int)$tile['id'].']" value="'.($tile['goldcost']).'"/></td>');
        $this->_print('<td valign="top"><input style="width:100px" name="grow_time['.(int)$tile['id'].']" value="'.$tile['grow_time'].'"/></td>');
        $this->_print('<td valign="top"><input style="width:100px" name="product_id['.(int)$tile['id'].']" value="'.$tile['product_id'].'"/></td>');
        $this->_print('<td valign="top"><input style="width:100px" name="product_num['.(int)$tile['id'].']" value="'.$tile['product_num'].'"/></td>');
        $this->_print('<td valign="top"><input style="width:100px" name="asset['.(int)$tile['id'].']" value="'.$tile['asset'].'"/></td>');
        $this->_print('<td valign="top"><input type="hidden" name="id['.(int)$tile['id'].']" value="'.$tile['id'].'" /><input style="width:50px" type="submit" name="delete['.$tile['id'].']" value="delete"/></td>');
        $this->_print('</tr>');

    }

    function out()
    {
        $this->_print('<!-- tile '.$this->id.' -->');
        // рисуем поле
        $tiles = $this->getTileList();
        $this->_print('<table cellpadding="0" cellspacing="0" border="0" >');
        $this->_print('<form method="post">');
        $this->_print('<tr>');
        $this->_print('<td width="107px"  valign="top" >&#160;</td>');
        $this->_print('<td width="200px" valign="top"><b>название</td>');
        $this->_print('<td width="100px" valign="top"><b>монет</td>');
        $this->_print('<td width="100px" valign="top"><b>золота</td>');
        $this->_print('<td width="120px" valign="top"><b>время роста, сек.</td>');
        $this->_print('<td width="120px" valign="top"><b>продукт</td>');
        $this->_print('<td width="120px" valign="top"><b>выход</td>');
        $this->_print('<td width="120px" valign="top"><b>ассет</td>');
        $this->_print('<td width="120px" valign="top">&#160;</td>');
        $this->_print('</tr>');
        foreach($tiles as $tile)
        {
            $this->print_tile($tile);
        }
        $this->print_tile(array('posX'=>-1,'posY'=>-1));
        $this->_print('<td colspan="7" valign="top" >
<input type="submit" value="save" name="save"/>
<input type="submit" value="json" name="json"/>
</td>');
        $this->_print('</table>');
        $this->_print('</form>');
        return $this->html;
    }
}
?>
